<?php

defined('SYSPATH') or die('No direct script access.');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Controller_Admin extends Controller_WebInterface {

    protected $_active_menu_left;
    protected $_user;

    public function before() {
        $this->setTemplate("admin");
        $this->setActive_menu_left('home');
        View::bind_global('active_menu_left', $this->_active_menu_left);
        parent::before();
        // check permission va session login
        $controller = strtolower($this->request->controller());
        $directory = strtolower($this->request->directory());
        $action = strtolower($this->request->action());
        $session_user = Controller_Helper_Authentication::get_session();

        View::bind_global('action', $action);
        // no check login
        if ($controller !== "auth") {
            $permission = Controller_Helper_Authentication::permission_admin($directory, $controller, $action);
            if (!$permission['permission']) {
                $message = Controller_Helper_Message::get_instance();
                $message->set_warning('permissin_denied', $this->getLanguage() . '/admin/warning');
                if (Arr::get($_REQUEST, 'ajax') == 1) {
                    echo "Authentication Fail";
                    die;
                }
                $this->redirect($permission['redirect']);
            }
        }
        $this->_user = $session_user;
        View::bind_global('_user', $session_user);
        //parellel add session for upload file

        if ($controller !== "auth") {
            $this->add_js(array('ace-extra.min', 'bootstrap.min', 'typeahead-bs2.min'));
            $this->add_css(array('ace-skins.min'));
        }
    }

    /**
     *
     * @param <type> $token 
     */
    public function check_security($token) {
        if ($token != Security::token()) {
            $this->redirect(URL::base() . 'admin/auth/login?logout=1');
        }
    }

}

?>
